package com.fcx.workai.jinchang.ui.mine_drawer.mysetpwd;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxh.component.basicore.Base.topbar.ActionBarConfig;
import com.hxh.component.business.app.App;
import com.hxh.component.business.ui.TopBarBaseLazyFragment;
import com.hxh.component.business.utils.Constant;

import com.fcx.workai.jinchang.R;


public class MineSetPwdFragment extends TopBarBaseLazyFragment<MineSetPwdPersenter> implements MineSetPwdContract.V
{

    private EditText et_pwd,et_realpwd;
    private Button btn_next;
    private EditText mEt_verifycode;
    private LinearLayout mLl_verify;
    private TextView mTv_getcode;
    private String mCur_mobile;

    public static MineSetPwdFragment newInstance() {

        Bundle args = new Bundle();

        MineSetPwdFragment fragment = new MineSetPwdFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine_setpwd;
    }

    @Override
    public void initData(Bundle saveInstanceState) {
        mCur_mobile = App.getUserDTO().getUser().getUser().getMobile();
        initView();
    }

    private void initView()
    {
        //do findview .... or init views
        btn_next = findViewBy(R.id.btn_next);
        et_pwd = findViewBy(R.id.et_pwd);
        et_realpwd = findViewBy(R.id.et_realpwd);
        mEt_verifycode = findViewBy(R.id.et_verifycode);
        mLl_verify = findViewBy(R.id.ll_verify);
        mTv_getcode = findViewBy(R.id.tv_getcode);

        mLl_verify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getP().getSmscode(mCur_mobile);
                timer.start();
                mTv_getcode.setTextColor(getRES_Color(R.color.text_blue));
                mLl_verify.setClickable(false);
                mLl_verify.setEnabled(false);
            }
        });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String verify_code = mEt_verifycode.getText().toString();
                String pwd = et_pwd.getText().toString();
                String pwd_real = et_realpwd.getText().toString();
                if (isEmpty(verify_code)) {
                    toastShort("请输入验证码");
                    return;
                }
                if (!isEmpty(pwd) && pwd.length() > 7) {
                    if(!isEmpty(pwd_real) && pwd_real.length()>7){
                        if (pwd.equals(pwd_real)) {
                            getP().setterPwd(mCur_mobile, verify_code, Constant.LOGINSCOPE_END_USER, pwd);
                        } else {
                            toastShort("两次密码不一致");
                        }
                    }else {
                        toastShort("登录密码长度必须大于7位！");
                    }
                } else {
                    toastShort("登录密码长度必须大于7位！");
                }
            }
        });
    }

    @Override
    public void showSmsResult() {

    }

    @Override
    public void showSetPwdResult(boolean isok) {
        toastShort("设置成功");
        getView().postDelayed(new Runnable() {
            @Override
            public void run() {
                pop();
            }
        },1000);
    }

    @Override
    public MineSetPwdPersenter newP() {
        return new MineSetPwdPersenter();
    }

    @Override
    protected ActionBarConfig setActionBarConfig() {
        return new ActionBarConfig.Builder()
                .enableBackView()
                .title("设置登录密码",R.color.black)
                .backgroundColor(R.color.white)
                .build();
    }

    /**
     * 验证码倒计时
     */
    private CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (mTv_getcode!=null){
                mTv_getcode.setText("重新获取(" + millisUntilFinished / 1000 + "秒)");
            }
        }

        @Override
        public void onFinish() {
            if (mTv_getcode!=null){
                mLl_verify.setEnabled(true);
                mLl_verify.setClickable(true);
                mTv_getcode.setText("重新获取");
                mTv_getcode.setTextColor(getRES_Color(R.color.colorAccent));
            }
        }
    };
}